/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Derek
 */
public class InboxServlet extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    String[] titles ={"This is a list of tasks that we made during design phase",
                        "If there are anything missing, please let us know",
                       "If there are blah"};
    
    String[] senders={"Kyu Lee","Kyu Lee","No idea"};
    
    String[] dates={"Nov 10th, 2007","Nov 10th, 2007","Nov 10th, 2007"};
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        //request.
        //HttpSession httpSession = request.getSession(true);
        //httpSession.setAttribute("usersession", ClassNet.INSTANCE.login("chenjinlin@hotmail.com", "password"));
        
        PrintWriter out = response.getWriter();
        try {
            
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Personal Inbox</title>");  
            //out.println("<LINK REL=\"stylesheet\" HREF=\"inbox.css\" TYPE=\"text/css\">");  
            out.println("<link rel=\"stylesheet\" href=\"inbox.css\" type=\"text/css\">");
            //generateInlineJS(out);
            //CSSGenerator.generateNewsGroupMainPageInlineCSS(out);
            
            out.println("</head>");
            out.println("<body>");
            //out.println("<h1>Servlet CourseNewsGroupServlet at " + request.getContextPath () + "</h1>");
            //out.println("<h1>Servlet get parameters at " + request.getParameter("testing") + "</h1>");
            out.println(
                    "<center>" +
                    "<form name=\"inboxForm\" method=\"get\" action=\"InboxServlet\">" +
                    "<table width=\"60%\" border class=\"messageTable\">" +// style=\"border-color:#336699 \" border=\""+ngroupOuterTableBorderWidth+"\">" +
                    "<tr border class=\"messageTR\">" +
                    "   <th  class=\"messageTH\">&nbsp</th>" +
                    "   <th align=\"left\" class=\"messageTH\">&nbsp</th>" +
                    "   <th align=\"left\" class=\"messageTH\"><p class=\"header\"> Subject</p></th>" +
                    "   <th align=\"left\" class=\"messageTH\"><p class=\"header\"> Sender </p></th>" +
                    "   <th align=\"left\" class=\"messageTH\"><p class=\"header\"> Date </p></th>" +
                    "</tr>" /*
                    "<tr border>" +
                    "   <td bgcolor=\"#E7F9F9\">&nbsp</td>" +
                    "   <td bgcolor=\"#E7F9F9\">&nbsp</td>" +
                    "   <td align=\"left\" bgcolor=\"#E7F9F9\"><p class=\"header\"> Subject</p></td>" +
                    "   <td align=\"left\" bgcolor=\"#E7F9F9\"><p class=\"header\"> Sender </p></td>" +
                    "   <td align=\"left\" bgcolor=\"#E7F9F9\"><p class=\"header\"> Date </p></td>" +
                    "</tr>" /*  */
                    );
            for(int i=0;i<titles.length;i++)
            {
                //TODO if undread use color #FDFBE7
                out.println(
                    "<tr border class=\"messageTR\">" +
                    "   <td  class=\"messageTD\"><img src=\"unread.gif\"></imag></td>" +
                    "   <td  class=\"messageTD\"><input type=\"checkbox\" name=\"inbox\" value=\""+i+"\">" +
                    "</td>" +
                    "   <td  class=\"messageTD\" align=\"left\" border bgcolor=\"#FDFBE7\">" +
                           "<p class=\"messageEntryUnread\">"+ titles[i]+ "</p></td>" +
                    "   <td   class=\"messageTD\" align=\"left\" border bgcolor=\"#FDFBE7\">" +
                           "<p class=\"messageEntryUnread\">"+senders[i] +"</p></td>" +
                    "   <td   class=\"messageTD\" align=\"left\" border bgcolor=\"#FDFBE7\">" +
                           "<p class=\"messageEntryUnread\">"+dates[i]+"</p></td>" +
                    "</tr>"    
                        
                        
                        
                        );
            
            }
            
        // end table tag
            out.println(        
                    //"</form>" +
                    "</table>" +
                    "<center>"
                    );
            
             out.println(
                    "<center>" +
                    //"<form name=\"inboxForm\" action=\"InboxServlet\">" +
                    "<table width=\"60%\" border class=\"actionsTable\">" +
                    "<tr>" +
                    " <td> <button onclick=\"" + //window.open(\"NewPersonalMessageServlet\")\">New</button>"+
                    "window.open(\'NewPersonalMessageServlet" +
                    "\', '_blank', 'width=500,height=400,status=1,menubar=0,resizable=0 '); return false;\">" +
                    "New</button>"+
                    "      <input type=\"submit\" name=\"Operation\" value=\"Unread\">" +
                    "      <input type=\"submit\" name=\"Operation\" value=\"Delete\">" +
                    " </td>" +
                    "</tr>" +
                    "</form>" +
                    "</table>" +
                    "</center>"
            );
            
            // end html
            out.println("</body>");
            out.println("</html>");
            
        } finally { 
            out.close();
        }
    } 
    private void handleUnRead(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException
    {
        response.setContentType("text/html;charset=UTF-8");
         PrintWriter out = response.getWriter();
       
        //ClassNet c = ClassNet.INSTANCE;
        try {
            
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet LoginServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Handling unread " + request.getContextPath () + "</h1>");
            out.println("<h1>Parameters " + request.getParameterValues("inbox")[0] +"</h1>");
            
            out.println("</body>");
            out.println("</html>");
            
        } finally { 
            out.close();
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        if(request.getParameter("Operation")==null)
            processRequest(request, response);
        else
            handleUnRead(request, response);
         //*/
        /*
        if(request.getParameter("Operation")!=null)
            handleOperation(request,response); // persist user operation to database
        
        processRequest(request, response);
         */
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
